package com.hdoit.apollo.web.cargo.apis;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.cargo.model.Standard;
import com.hdoit.apollo.logical.cargo.model.StandardProperty;
import com.hdoit.apollo.logical.cargo.model.StandardPropertyUser;
import com.hdoit.apollo.logical.cargo.model.StandardUser;
import com.hdoit.apollo.logical.cargo.service.IStandardPropertyService;
import com.hdoit.apollo.logical.cargo.service.IStandardPropertyUserService;
import com.hdoit.apollo.logical.cargo.service.IStandardService;
import com.hdoit.apollo.logical.cargo.service.IStandardUserService;
import com.hdoit.apollo.logical.viewmodel.cargo.StandardSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;


@Controller
@RequestMapping(value = "/api/v1/standard/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/standard")
public class StandardApi extends BaseApi{
	
	@Autowired
	private IStandardService standardService;
	
	@Autowired
	private IStandardUserService standardUserService;
	
	@Autowired
	private IStandardPropertyUserService standardPropertyUserService;
	
	@Autowired
	private IStandardPropertyService standardPropertyService;
	
	private final String getStandardListApi="getStandardList";
	
	private final String saveStandardApi="saveStandard";
	
	private final String saveStandardPropertyApi="saveStandardProperty";
	
	private final String getStandardPropertyListApi="getStandardPropertyList";
	
	private final String getStandardUserByCargoIdApi="getStandardUserByCargoId";
	private final String getStandardPropertyUserByCargoIdApi="getStandardPropertyUserByCargoId";
	
	@RequestMapping(value = getStandardPropertyUserByCargoIdApi)
    @ApiOperation(notes = getStandardPropertyUserByCargoIdApi, httpMethod = "GET", value = "属性使用信息")
    public @ResponseBody String getStandardPropertyUserByCargoId(String cargoId){
    	List<StandardPropertyUser> standardPropertyUsers = standardPropertyUserService.queryListByCargoId(cargoId);
    	
    	ResponseModel rm=new ResponseModel();
    	rm.put("standardPropertyUsers",standardPropertyUsers);
    	System.err.println(standardPropertyUsers.size()+"lllllllllllllllllllllllll");
    	return rm.toJson();
    }
	
	
	@RequestMapping(value = getStandardUserByCargoIdApi)
    @ApiOperation(notes = getStandardUserByCargoIdApi, httpMethod = "GET", value = "规格使用信息")
    public @ResponseBody String getStandardUserByCargoId(String cargoId){
    	List<StandardUser> standardUsers = standardUserService.queryListByCargoId(cargoId);
    	
    	ResponseModel rm=new ResponseModel();
    	rm.put("standardUsers",standardUsers);
    	System.err.println(standardUsers.size()+"22222222222222222222222222");
    	return rm.toJson();
    }
	
 	@RequestMapping(value = getStandardListApi)
    @ApiOperation(notes = getStandardListApi, httpMethod = "GET", value = "规格列表")
    public @ResponseBody String getStandardList(){
 		StandardSearch search=new StandardSearch();
    	List<Standard> standards = standardService.queryListMP(search,null);
    	
    	ResponseModel rm=new ResponseModel();
    	rm.put("standards",standards);
    	return rm.toJson();
    }
	    
	 	
	 	@RequestMapping(value = getStandardPropertyListApi)
	    @ApiOperation(notes = getStandardPropertyListApi, httpMethod = "GET", value = "规格属性列表")
	    public @ResponseBody String getStandardPropertyList(String standardId){
	    	List<StandardProperty> standardProperties = standardPropertyService.queryListByStandardId(standardId);
	    	
	    	ResponseModel rm=new ResponseModel();
	    	rm.put("standardProperties",standardProperties);
	    	return rm.toJson();
	    }
	    
	    
	    @RequestMapping(value = saveStandardApi)
	    @ApiOperation(notes = saveStandardApi, httpMethod = "POST", value = "规格保存")
	    public @ResponseBody String saveStandard(Standard standard){
	    	Standard standarded=standardService.queryModel(standard.getStandardId());
	    	if (standarded==null) {
	    		standard.setCreateDate(DateUtils.getCurrent());
	    		standard.setCreateUserId(getLoginId());
	    		standard.setCreateUserName(getLoginName());
	    		standard.setDeleteMark(YesOrNoInteger.no);
	    		standard.setModelStatus(ModelStatus.add);
			}else {
				standard.setModifyDate(DateUtils.getCurrent());
				standard.setModifyUserId(getLoginId());
				standard.setModifyUserName(getLoginName());
				standard.setModelStatus(ModelStatus.update);
				standard.setDeleteMark(standarded.getDeleteMark());
			}
	    	
	    	
	    	
	     	ResponseModel rm=new ResponseModel();
	     	if(!standardService.applyChange(standard)){
	     		rm.setStatus(ResponseStatus.dataError);
	     	}
	     	return rm.toJson();
	    }
	    
	    
	    @RequestMapping(value = saveStandardPropertyApi)
	    @ApiOperation(notes = saveStandardPropertyApi, httpMethod = "POST", value = "规格属性保存")
	    public @ResponseBody String saveStandardProperty(StandardProperty standardProperty){
	    	StandardProperty standardPropertyed=standardPropertyService.queryModel(standardProperty.getPropertyId());
	    	if (standardPropertyed==null) {
	    		standardProperty.setCreateDate(DateUtils.getCurrent());
	    		standardProperty.setCreateUserId(getLoginId());
	    		standardProperty.setCreateUserName(getLoginName());
	    		standardProperty.setDeleteMark(YesOrNoInteger.no);
	    		standardProperty.setModelStatus(ModelStatus.add);
			}else {
				standardProperty.setModifyDate(DateUtils.getCurrent());
				standardProperty.setModifyUserId(getLoginId());
				standardProperty.setModifyUserName(getLoginName());
				standardProperty.setModelStatus(ModelStatus.update);
				standardProperty.setDeleteMark(standardPropertyed.getDeleteMark());
			}
	    	
	     	ResponseModel rm=new ResponseModel();
	     	if(!standardPropertyService.applyChange(standardProperty)){
	     		rm.setStatus(ResponseStatus.dataError);
	     	}
	     	
	     	return rm.toJson();
	    }

}
